package coop.bancocredicoop.proyectos.gd;

import java.util.ArrayList;
import java.util.List;

public class Pager {

	private int offset;
	private int itemsPerPage;
	private int currentPageNumber;
	private int totalItems;
	
	public Pager(int offset, int itemsPerPage, int totalItems) {
		this.offset = offset;
		this.itemsPerPage = itemsPerPage;
		this.totalItems = totalItems;
		this.currentPageNumber = (int) (Math.ceil((double) offset / (double) itemsPerPage) + 1);
	}
	
	public int getTotalItems() {
		return totalItems;
	}
	
	public boolean getIsFirstPage() {
		return currentPageNumber == 1;
	}
	
	public int getCurrentPageNumber() {
		return currentPageNumber;
	}
	
	public int getItemsPerPage() {
		return itemsPerPage;
	}
	
	public int getLastPageNumber() {
		int lastPageIndex = (int) Math.floor((double) totalItems / (double) itemsPerPage);
		return lastPageIndex == 0 ? 1 : lastPageIndex;
	}
	
	public int getOffset() {
		return offset;
	}
	
	public boolean getIsLastPage() {
		return currentPageNumber == getLastPageNumber();
	}
	
	public List<Integer> getPageOffsets() {
		List<Integer> offsets = new ArrayList<Integer>(getLastPageNumber());
		for (int i=0; i < getLastPageNumber(); i++) {
			offsets.add(i * itemsPerPage);
		}
		return offsets;
	}
	
}
